package com.tmptSource.controller.action;

import com.opensymphony.xwork2.ActionSupport;
import com.tmpt.dto.Result;
import com.tmpt.utils.report.ExcelUtils;
import com.tmpt.utils.report.PdfUtils;
import com.tmptSource.dto.A2userDto;
import com.tmptSource.dto.OwrDoubleTaskPairDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.service.intf.IOwrDoubleTaskPairService;
import com.tmpt.utils.*;
import com.tmptSource.service.intf.IOwrTmpPairService;
import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

@Getter
@Setter
@Controller(value="owrDoubleTaskPairAction")
@Scope("prototype")
public class OwrDoubleTaskPairAction extends ActionSupport {
	private Logger log = Logger.getLogger(this.getClass());


	//------------
	//与页面交互的分页和排序
	//--------------
	private int page=0;
	private int rows=0;
	private String sortColumn;
	private String sortOrder;

	//------------
	//与页面交互的业务数据
	//--------------
    private String id;
    private String deleteflag;
    private String createdate;
	private String createdate_Start;
	private String createdate_End;
    private String createman;
    private String modifydate;
	private String modifydate_Start;
	private String modifydate_End;
    private String modifyman;
    private String createmanId;
    private String modifymanId;
    private String companyId;
    private String owrRegionId;
    private String regionName;
    private String quarter;
	private String year;
    private String userId;
    private String entityId;
    private String owrType;
    private String month;
	private String owTaskStatus;
	private String userName;
	private String entityName;
	private String owTaskId;

	public String getOwTaskId() {
		return owTaskId;
	}

	public void setOwTaskId(String owTaskId) {
		this.owTaskId = owTaskId;
	}

	//返回结果
	private String result;
	//返回结果
	private List<Map<String,Object>> jsonArrayResult = new ArrayList<Map<String,Object>>();


	@Resource
	private IOwrDoubleTaskPairService owrDoubleTaskPairService;
    @Resource
    private IOwrTmpPairService iOwrTmpPairService;




	/**
	* 分页查询列表
	* @return
	*/
	public String queryListBySearch(){
		try {

			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//查询参数
			//分页/排序查询条件
			PageDto pageDto = new PageDto();
			//默认主键desc
			if(StringUtils.isNull(sortColumn)){
				sortColumn = "id";
				sortOrder = "desc";
			}
			pageDto.setPageCurrent(page-1);
			pageDto.setPageSize(rows);
			pageDto.setSortColumn(sortColumn);
			pageDto.setSortOrder(sortOrder);

			//查询条件
			OwrDoubleTaskPairDto owrDoubleTaskPairParam = new OwrDoubleTaskPairDto();
			owrDoubleTaskPairParam.setCreatedate_Start(this.createdate_Start);
			owrDoubleTaskPairParam.setCreatedate_End(this.createdate_End);
			owrDoubleTaskPairParam.setCreatedate(this.createdate);
			owrDoubleTaskPairParam.setCreateman(this.createman);
			owrDoubleTaskPairParam.setModifyman(this.modifyman);
			owrDoubleTaskPairParam.setOwrRegionId(this.owrRegionId);
			owrDoubleTaskPairParam.setRegionName(this.regionName);
			owrDoubleTaskPairParam.setQuarter(this.quarter);
			owrDoubleTaskPairParam.setYear(this.year);
			owrDoubleTaskPairParam.setUserId(this.userId);
			owrDoubleTaskPairParam.setEntityId(this.entityId);
			owrDoubleTaskPairParam.setOwrType(this.owrType);
			owrDoubleTaskPairParam.setMonth(this.month);
			owrDoubleTaskPairParam.setTaskStatusQ(this.owTaskStatus);
			owrDoubleTaskPairParam.setUserName(this.userName);
			owrDoubleTaskPairParam.setEntityName(this.entityName);
			//owTaskId==0时，就是查询未派发的任务清单
			owrDoubleTaskPairParam.setOwTaskId(this.owTaskId);


			//data limit control
			String requireUrl = request.getRequestURI().toString();
			String queryString = request.getQueryString();
			String contextPath = request.getContextPath();
			//data limit parameters
			String userId = WebUtils.getUserId();
			Map<String,Object> paramMap = new HashMap<String, Object>();
			paramMap.put("userId", userId);
			paramMap.put("contextPath", contextPath);
			paramMap.put("requireUrl", requireUrl);
			paramMap.put("queryString", queryString);

			//调用远程服务
			Result<OwrDoubleTaskPairDto> resultObj =  owrDoubleTaskPairService.pagination(owrDoubleTaskPairParam,pageDto,paramMap);
			if(!resultObj.isSuccess()){
				this.result = "error:"+resultObj.getContent();
				JsonPaginationUtils.responseWriter(response, this.result);
				return "jsonResult";
			}
			Map<String,Object> resultMap = resultObj.getResultMap();

			//分页记录
			List<OwrDoubleTaskPairDto> list = (List<OwrDoubleTaskPairDto>)resultMap.get("rows");
			JSONArray jsonArrayRows = JSONArray.fromObject(list);

			//分页总记录数
			int total = 0;
			if(resultMap.get("total") != null){
				total = Integer.parseInt(resultMap.get("total").toString());
			}

			//分页合计（select '合计' as c1, sum(balance) as c2 ....）
			JSONArray jsonArrayFooter = null;
			if(resultMap.get("footer")!=null){
				jsonArrayFooter = JSONArray.fromObject(resultMap.get("footer"));
			}

			JSONObject json = new JSONObject();
			json.put("total", total);
			json.put("rows", jsonArrayRows);
			//json.put("reportSqlString",reportSqlString);
			if(jsonArrayFooter!=null){
				json.put("footer", jsonArrayFooter);
			}

			this.result = json.toString();
			JsonPaginationUtils.responseWriter(response, this.result);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "jsonResult";
	}

	/**
	 * 双随机-餐厨生成任务清单-手动
	 * @return
	 */
	public String createTaskListKitchen(){


		Result<OwrDoubleTaskPairDto> resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//插入参数
			OwrDoubleTaskPairDto owrDoubleTaskPairDto = new OwrDoubleTaskPairDto();
			owrDoubleTaskPairDto.setYear(this.year);
			owrDoubleTaskPairDto.setQuarter(this.quarter);
			owrDoubleTaskPairDto.setMonth(this.month);

			//调用远程服务
			resultObj =  iOwrTmpPairService.exeKitchenQuartz(owrDoubleTaskPairDto);
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}
		return "jsonArray";
	}



	/**
	 * 双随机-工地生成任务清单-手动
	 * @return
	 */
	public String createTaskListBuilding(){

		Result<OwrDoubleTaskPairDto> resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//插入参数
			OwrDoubleTaskPairDto owrDoubleTaskPairDto = new OwrDoubleTaskPairDto();
			owrDoubleTaskPairDto.setYear(this.year);
			owrDoubleTaskPairDto.setQuarter(this.quarter);
			owrDoubleTaskPairDto.setMonth(this.month);

			//调用远程服务
			resultObj =  iOwrTmpPairService.exeBuildingQuartz(owrDoubleTaskPairDto);
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}
		return "jsonArray";
	}

	/**
	 * 双随机-工地和餐厨派发任务清单-手动
	 * @return
	 */
	public String sendToTask(){

		Result<OwrDoubleTaskPairDto> resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			String adminMan = WebUtils.getUsername();//加入管理员
			String userId = WebUtils.getUserId();
			//插入参数
			OwrDoubleTaskPairDto owrDoubleTaskPairDto = new OwrDoubleTaskPairDto();
			owrDoubleTaskPairDto.setYear(this.year);
			owrDoubleTaskPairDto.setQuarter(this.quarter);
			owrDoubleTaskPairDto.setMonth(this.month);
			owrDoubleTaskPairDto.setModifymanId(userId);
			owrDoubleTaskPairDto.setModifyman(adminMan);
			owrDoubleTaskPairDto.setCompanyId(WebUtils.getCompanyId());
			owrDoubleTaskPairDto.setOwrType(this.owrType);

			//调用远程服务
			resultObj =  iOwrTmpPairService.sendToTask(owrDoubleTaskPairDto);
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}
		return "jsonArray";
	}

	/**
	 * 双随机报表
	 * @return
	 */
	public String download(){
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//调用远程服务
			Result<OwrDoubleTaskPairDto> resultObj =  owrDoubleTaskPairService.selectById(Long.parseLong(id));
			OwrDoubleTaskPairDto owrDoubleTaskPairDto = resultObj.getResultObj();

			String quarterStr = "";
			String quarter = owrDoubleTaskPairDto.getQuarter();
			if("Q1".equals(quarter)){
				quarterStr = "一";
			}
			if("Q2".equals(quarter)){
				quarterStr = "二";
			}
			if("Q3".equals(quarter)){
				quarterStr = "三";
			}
			if("Q4".equals(quarter)){
				quarterStr = "四";
			}
			//报表
			ExcelUtils.downloadReport4DoubleRandom("第"+quarterStr+"季度双随机报表(陆家嘴上报)",owrDoubleTaskPairDto,response);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;//"jsonResult";
	}

	/**
	 * 双随机报表-批量
	 * @return
	 */
	public String downloadBatch(){
		try {
			page = 1;
			rows = 1000000;

			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//查询参数
			//分页/排序查询条件
			PageDto pageDto = new PageDto();
			//默认主键desc
			if (StringUtils.isNull(sortColumn)) {
				sortColumn = "id";
				sortOrder = "desc";
			}
			pageDto.setPageCurrent(page - 1);
			pageDto.setPageSize(rows);
			pageDto.setSortColumn(sortColumn);
			pageDto.setSortOrder(sortOrder);

			//查询条件
			OwrDoubleTaskPairDto owrDoubleTaskPairParam = new OwrDoubleTaskPairDto();
			owrDoubleTaskPairParam.setOwrRegionId(this.owrRegionId);
			owrDoubleTaskPairParam.setQuarter(this.quarter);
			owrDoubleTaskPairParam.setYear(this.year);
			owrDoubleTaskPairParam.setOwrType(this.owrType);
			owrDoubleTaskPairParam.setMonth(this.month);
			//下载已处理双随机报表
			owrDoubleTaskPairParam.setTaskStatusQ("20");

			//调用远程服务
			Result<OwrDoubleTaskPairDto> resultObj = owrDoubleTaskPairService.pagination(owrDoubleTaskPairParam, pageDto);
			if (!resultObj.isSuccess()) {
				this.result = "error:" + resultObj.getContent();
				JsonPaginationUtils.responseWriter(response, this.result);
				return "jsonResult";
			}
			Map<String, Object> resultMap = resultObj.getResultMap();

			//分页记录
			List<OwrDoubleTaskPairDto> list = (List<OwrDoubleTaskPairDto>) resultMap.get("rows");

			//分页总记录数
			int total = 0;
			if (resultMap.get("total") != null) {
				total = Integer.parseInt(resultMap.get("total").toString());
			}

			String yearStr = dealYear(this.year);

			String quarterStr = dealQuarter(this.quarter);

			String monthStr = dealMonth(this.month);

			//报表
			PdfUtils.downloadReport4DoubleRandomPDFBatch(yearStr+quarterStr+monthStr+"双随机报表(陆家嘴上报)",list,response);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;//"jsonResult";
	}

	private String dealYear(String year){
		String yearStr = "";
		if((!"".equals(year))&&(null!=year)){
			yearStr = year+"年";
		}
		return yearStr;
	}
	private String dealQuarter(String quarter){
		String quarterStr="";
		if("Q1".equals(quarter)){
			quarterStr = "第一季度";
		}
		if("Q2".equals(quarter)){
			quarterStr = "第二季度";
		}
		if("Q3".equals(quarter)){
			quarterStr = "第三季度";
		}
		if("Q4".equals(quarter)){
			quarterStr = "第四季度";
		}
		return quarterStr;
	}
	private String dealMonth(String month){
		String monthStr = "";
		if("01".equals(month)){
			monthStr = "01月";
		}
		if("02".equals(month)){
			monthStr = "02月";
		}
		if("03".equals(month)){
			monthStr = "03月";
		}
		if("04".equals(month)){
			monthStr = "04月";
		}
		if("05".equals(month)){
			monthStr = "05月";
		}
		if("06".equals(month)){
			monthStr = "06月";
		}
		if("07".equals(month)){
			monthStr = "07月";
		}
		if("08".equals(month)){
			monthStr = "08月";
		}
		if("09".equals(month)){
			monthStr = "09月";
		}
		if("10".equals(month)){
			monthStr = "10月";
		}
		if("11".equals(month)){
			monthStr = "11月";
		}
		if("12".equals(month)){
			monthStr = "12月";
		}
		return monthStr;
	}
	/**
	* 伪删除数据
	* @return
	*/
	public String removeData(){
		Result resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//参数
			String adminMan = WebUtils.getUsername();//加入管理员

			//调用远程服务
			resultObj =  owrDoubleTaskPairService.deleteById(Long.valueOf(id),adminMan);
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}

		return "jsonArray";
	}


	/**
	* 编辑数据
	* @return
	*/
	public String editData(){
		Result<OwrDoubleTaskPairDto> resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//参数
			OwrDoubleTaskPairDto owrDoubleTaskPairDto = new OwrDoubleTaskPairDto();
			String adminMan = WebUtils.getUsername();//加入管理员
            String userId = WebUtils.getUserId();
			owrDoubleTaskPairDto.setModifymanId(userId);
			owrDoubleTaskPairDto.setModifyman(adminMan);
			owrDoubleTaskPairDto.setCompanyId(WebUtils.getCompanyId());
			owrDoubleTaskPairDto.setId(this.id);
			//编辑数据
			owrDoubleTaskPairDto.setOwrRegionId(this.owrRegionId);
			owrDoubleTaskPairDto.setRegionName(this.regionName);
			owrDoubleTaskPairDto.setQuarter(this.quarter);
			owrDoubleTaskPairDto.setYear(this.year);
			owrDoubleTaskPairDto.setUserId(this.userId);
			owrDoubleTaskPairDto.setEntityId(this.entityId);

			//调用远程服务
			resultObj =  owrDoubleTaskPairService.editData(owrDoubleTaskPairDto);
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}

		return "jsonArray";
	}

	/**
	* 添加数据
	* @return
	*/
	public String addData(){
		Result<OwrDoubleTaskPairDto> resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//插入参数
			OwrDoubleTaskPairDto owrDoubleTaskPairDto = new OwrDoubleTaskPairDto();
			String adminMan = WebUtils.getUsername();//加入管理员
			String userId = WebUtils.getUserId();
			owrDoubleTaskPairDto.setCreatemanId(userId);
			owrDoubleTaskPairDto.setCreateman(adminMan);
			owrDoubleTaskPairDto.setCompanyId(WebUtils.getCompanyId());
			//插入数据
			owrDoubleTaskPairDto.setOwrRegionId(this.owrRegionId);
			owrDoubleTaskPairDto.setRegionName(this.regionName);
			owrDoubleTaskPairDto.setQuarter(this.quarter);
			owrDoubleTaskPairDto.setYear(this.year);
			owrDoubleTaskPairDto.setUserId(this.userId);
			owrDoubleTaskPairDto.setEntityId(this.entityId);

			//调用远程服务
			resultObj =  owrDoubleTaskPairService.addData(owrDoubleTaskPairDto);
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}
		return "jsonArray";
	}


	//=====================================================
	// setter getter method
	//=====================================================


	public String getId(){
	return id;
	}
	public void setId(String id){
	this.id = id;
	}
	public String getDeleteflag(){
	return deleteflag;
	}
	public void setDeleteflag(String deleteflag){
	this.deleteflag = deleteflag;
	}
	public String getCreatedate(){
	return createdate;
	}
	public void setCreatedate(String createdate){
	this.createdate = createdate;
	}
    public String getCreatedate_Start(){
    	return createdate_Start;
    }
    public void setCreatedate_Start(String createdate_Start){
    	this.createdate_Start = createdate_Start;
    }
    public String getCreatedate_End(){
    	return createdate_End;
    }
    public void setCreatedate_End(String createdate_End){
    	this.createdate_End = createdate_End;
    }
	public String getCreateman(){
	return createman;
	}
	public void setCreateman(String createman){
	this.createman = createman;
	}
	public String getModifydate(){
	return modifydate;
	}
	public void setModifydate(String modifydate){
	this.modifydate = modifydate;
	}
    public String getModifydate_Start(){
    	return modifydate_Start;
    }
    public void setModifydate_Start(String modifydate_Start){
    	this.modifydate_Start = modifydate_Start;
    }
    public String getModifydate_End(){
    	return modifydate_End;
    }
    public void setModifydate_End(String modifydate_End){
    	this.modifydate_End = modifydate_End;
    }
	public String getModifyman(){
	return modifyman;
	}
	public void setModifyman(String modifyman){
	this.modifyman = modifyman;
	}
	public String getCreatemanId(){
	return createmanId;
	}
	public void setCreatemanId(String createmanId){
	this.createmanId = createmanId;
	}
	public String getModifymanId(){
	return modifymanId;
	}
	public void setModifymanId(String modifymanId){
	this.modifymanId = modifymanId;
	}
	public String getCompanyId(){
	return companyId;
	}
	public void setCompanyId(String companyId){
	this.companyId = companyId;
	}
	public String getOwrRegionId(){
	return owrRegionId;
	}
	public void setOwrRegionId(String owrRegionId){
	this.owrRegionId = owrRegionId;
	}
	public String getRegionName(){
	return regionName;
	}
	public void setRegionName(String regionName){
	this.regionName = regionName;
	}
	public String getQuarter(){
	return quarter;
	}
	public void setQuarter(String quarter){
	this.quarter = quarter;
	}
	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}
	public String getUserId(){
	return userId;
	}
	public void setUserId(String userId){
	this.userId = userId;
	}
	public String getEntityId(){
	return entityId;
	}
	public void setEntityId(String entityId){
	this.entityId = entityId;
	}

	public String getSortColumn() {
		return sortColumn;
	}
	public void setSortColumn(String sortColumn) {
		this.sortColumn = sortColumn;
	}
	public String getSortOrder() {
		return sortOrder;
	}
	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public List<Map<String, Object>> getJsonArrayResult() {
		return jsonArrayResult;
	}
	public void setJsonArrayResult(List<Map<String, Object>> jsonArrayResult) {
		this.jsonArrayResult = jsonArrayResult;
	}

	public String getOwrType() {
		return owrType;
	}

	public void setOwrType(String owrType) {
		this.owrType = owrType;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getOwTaskStatus() {
		return owTaskStatus;
	}

	public void setOwTaskStatus(String owTaskStatus) {
		this.owTaskStatus = owTaskStatus;
	}
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getEntityName() {
		return entityName;
	}

	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}
}
